﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Controls;

namespace Qplus.UI
{
    public partial class RichTextControl : UserControl
    {
        SmileyControl smiley = new SmileyControl();
        TextBox textTxt;
        TextBlock textTBK;
        bool isinput = false;
        int backKeyTwice = 0;
        public RichTextControl()
        {
            InitializeComponent();
            WrapPanel tmp = this.Parent as WrapPanel;
            //if (this.Height < 80)
            //{
            //    contentSV.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
            //    contentSV.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
            //}
        }
        public bool IsInput
        {
            set
            {
                isinput = value;
                if(value)
                {
                    this.MaxHeight = 50;
                    this.LayoutRoot.Background = new SolidColorBrush(Colors.White);
                    richTxtCtrlSV.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
                    richTxtCtrlSV.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
                }
            }
            get { return isinput; }
        }
        public TextBox Txt
        {
            set
            {
                if (IsInput == true)
                {
                    this.MaxHeight = 70;
                    textTxt = value;
                    textTxt.Margin = new Thickness(0);
                    textTxt.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    textTxt.Foreground = new SolidColorBrush(Colors.Red);
                    textTxt.KeyUp += new KeyEventHandler(textTxt_KeyUp);
                    richTxtCtrlWP.Children.Add(textTxt);
                }
                else
                {
                    textTBK = new TextBlock() { Text = value.Text};
                    textTBK.Margin = new Thickness(0);
                    richTxtCtrlWP.Children.Add(textTBK);
                }
            }
        }
        void textTxt_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tmpTB = sender as TextBox;
            WrapPanel tmpWP = tmpTB.Parent as WrapPanel;
            Key tmpKey = e.Key;
            int indexofsmiley = tmpWP.Children.IndexOf(tmpTB) - 1;
            SmileyControl tmpSC = tmpWP.Children.ElementAt(indexofsmiley) as SmileyControl;
            if (tmpKey == Key.Left && tmpTB.SelectionStart == 0)
            {
                tmpSC.Focus();
            }

            //if (tmpKey == Key.Back && tmpTB.SelectionStart==0)
            //{
            //    backKeyTwice++;
            //    tmpWP.Children.RemoveAt(indexofpic);
            //    TextBox tmp = tmpWP.Children.ElementAt(indexofpic - 1) as TextBox;
            //    tmp.Focus();
            //    tmp.SelectionStart = tmp.Text.Length;
            //    if (tmpTB.Text == "")
            //    {
            //        tmpWP.Children.Remove(tmpTB);
            //        backKeyTwice = 0;
            //    }
            //}
        }
        public SmileyControl Smiley
        {
            set
            {
                if (IsInput == true)
                {
                    this.MaxHeight = 70;
                }
                if (value != null)
                {
                    richTxtCtrlWP.Children.Add(value);
                }
            }
        }

        public BitmapImage Pic
        {
            set;
            get;
            //{
            //    smiley.Source = value;
            //    Image tmpImg = new Image();
            //    tmpImg.Source = smiley.Source;
            //    tmpImg.Stretch = Stretch.None;
            //    richTxtCtrlWP.Children.Add(tmpImg);
            //}
            //get
            //{
            //    if (smiley.Source != null)
            //        return (BitmapImage)smiley.Source;
            //    else
            //        return null;
            //}
        }

        private void richTxtCtrlWP_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            richTxtCtrlSV.ScrollToHorizontalOffset(richTxtCtrlWP.ActualWidth);
        }

        public void contentCopy(RichTextControl viewRTC)
        {
            //viewRTC.richTxtCtrlWP = this.richTxtCtrlWP;
            int count = this.richTxtCtrlWP.Children.Count;
            for (int i = 0; i < count; i++)
            {
                var node = this.richTxtCtrlWP.Children.ElementAt(i);
                var txtType = node as TextBox;
                var smileyType = node as SmileyControl;
                if (txtType != null)
                {
                    TextBox tmp = new TextBox() { Text = txtType.Text, Foreground = new SolidColorBrush(Colors.Black), Style = (Style)App.Current.Resources["noBorderTB"] };
                    viewRTC.richTxtCtrlWP.Children.Add(tmp);
                }
                if (smileyType != null)
                {
                    SmileyControl tmp = new SmileyControl();
                    tmp.Index = smileyType.Index;
                    viewRTC.Smiley = tmp;
                }
            }

            this.richTxtCtrlWP.Children.Clear();
        }

        private void UserControl_GotFocus(object sender, RoutedEventArgs e)
        {
            //TextBox tmpTxt = this.richTxtCtrlWP.Children.ElementAt(this.richTxtCtrlWP.Children.Count - 1) as TextBox;
            //if (tmpTxt != null) tmpTxt.Focus();
            //SmileyControl tmpSmiley = this.richTxtCtrlWP.Children.ElementAt(this.richTxtCtrlWP.Children.Count - 1) as SmileyControl;
            //if (tmpSmiley != null) tmpSmiley.Focus();
        }
    }
}
